<!--右侧预览元素-->
<template>
  <n-space class="go-mt-0" :wrap="false">
    <n-button v-for="item in comBtnList" :key="item.title" :type="item.type" ghost @click="item.event">
      <template #icon>
        <component :is="item.icon"></component>
      </template>
      <span>{{ item.title }}</span>
    </n-button>
  </n-space>
</template>

<script setup lang="ts">
import {computed} from 'vue'
import {
  renderIcon,
  goDialog,
  fetchPathByName,
  routerTurnByPath,
  setSessionStorage,
  getLocalStorage,
  JSONStringify
} from '@/utils'
import {PreviewEnum} from '@/enums/pageEnum'
import {StorageEnum} from '@/enums/storageEnum'
import {useRoute} from 'vue-router'
import {useChartEditStore} from '@/store/modules/chartEditStore/chartEditStore'
import {syncData} from '../../ContentEdit/components/EditTools/hooks/useSyncUpdate.hook'
import {icon} from '@/plugins'
import {cloneDeep} from 'lodash'
// @ts-ignore
import UserPageApi from '@/api/flow/UserPageApi.js'

const {BrowsersOutlineIcon, SendIcon, AnalyticsIcon} = icon.ionicons5
const chartEditStore = useChartEditStore()

const routerParamsInfo = useRoute()

// 编辑界面预览
const previewHandle = () => {
  const path = fetchPathByName(PreviewEnum.CHART_PREVIEW_NAME, 'href')
  if (!path) return
  const {id} = routerParamsInfo.params
  // id 标识
  const previewId = typeof id === 'string' ? id : id[0] // gg
  const storageInfo = chartEditStore.getStorageInfo()
  const sessionStorageInfo = getLocalStorage(StorageEnum.GO_CHART_STORAGE_LIST) || []

  let Result: { id: string }[] = [];
  if (sessionStorageInfo?.length) {
    const repeateIndex = sessionStorageInfo.findIndex((e: { id: string }) => e.id === previewId)
    // 重复替换
    if (repeateIndex !== -1) {
      sessionStorageInfo.splice(repeateIndex, 1, {id: previewId, ...storageInfo})
      console.log('检查中4...')
      setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
    } else {
      sessionStorageInfo.push({
        id: previewId,
        ...storageInfo
      })
      console.log('检查中5...')
      setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
    }
    Result = sessionStorageInfo
  } else {
    Result = [{id: previewId, ...storageInfo}]
    console.log('检查中6...')
    const CurrentEditPage = 'default'
    const upa = new UserPageApi()
    //setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, Result)
    //upa.SaveUserPageComponents(previewId, CurrentEditPage + '.Component', JSONStringify(Result, 0), () => {
    //})
  }
  // SaveUserPageComponents(Result)
  // 跳转
  routerTurnByPath(path, [previewId], undefined, false)
}

// 发布
const sendHandle = () => {
  goDialog({
    message: '开发中...',
    positiveText: '了然',
    closeNegativeText: true,
    onPositiveCallback: () => {
    }
  })
}

const btnList = [
  {
    select: true,
    title: '同步内容',
    type: 'primary',
    icon: renderIcon(AnalyticsIcon),
    event: syncData
  },
  {
    select: true,
    title: '预览',
    icon: renderIcon(BrowsersOutlineIcon),
    event: previewHandle
  },
  {
    select: true,
    title: '发布',
    icon: renderIcon(SendIcon),
    event: sendHandle
  }
]

const comBtnList = computed(() => {
  if (chartEditStore.getEditCanvas.isCodeEdit) {
    return btnList
  }
  const cloneList = cloneDeep(btnList)
  cloneList.shift()
  return cloneList
})
</script>

<style lang="scss" scoped>
.align-center {
  margin-top: -4px;
}
</style>
